<?php
require_once 'Zend/Config/Ini.php';
require_once 'Zend/Db/Adapter/Pdo/Mysql.php';

class DBManager {

    private $log;
    private $db;

    public function getDb() {
        return $this->db;
    }

    public function setDb($db) {
        $this->db = $db;
    }

    public function __construct($logFile) {
        $this->log = $logFile;
        $config = new Zend_Config_Ini(CONFIGURATION_FILE, APPLICATION_ENV);
        try {
            $this->db = new Zend_Db_Adapter_Pdo_Mysql(array('host' => $config->get("dbhost"),
                'username' => $config->get("dbusername"),
                'password' => $config->get("dbpassword"),
                'dbname' => $config->get("dbname")));

            $this->log->debug("[dbconnection] " . implode(" | ", array('host' => $config->get("dbhost"),
                        'username' => $config->get("dbusername"),
                        'password' => $config->get("dbpassword"),
                        'dbname' => $config->get("dbname"))));
        } catch (Exception $e) {
            $this->log->err("connection error - " . implode(" | ", array('host' => $config->get("dbhost"),
                        'username' => $config->get("dbusername"),
                        'password' => $config->get("dbpassword"),
                        'dbname' => $config->get("dbname"))));
            $this->log->err($e->getTraceAsString());
        }
    }

    public function close() {
        try {
            $this->db->closeConnection();
        } catch (Exception $e) {
            $this->log->err($e->getTraceAsString());
        }
    }

}
?>
\